package com.wangxinyuchuan.demo.controller;

import com.wangxinyuchuan.demo.constant.DiscountVolumeEnum;
import com.wangxinyuchuan.demo.model.DiscountVolume;
import com.wangxinyuchuan.demo.service.DiscountVolumeQueryService;
import com.wangxinyuchuan.demo.template.DisCountVolumeManager;
import com.wangxinyuchuan.demo.template.impl.QQVipStrategy;
import com.wangxinyuchuan.demo.template.impl.RedPaperStrategy;
import com.wangxinyuchuan.demo.template.impl.ShoppingStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private DiscountVolumeQueryService discountVolumeQueryService;


    @GetMapping("/t1")
    public DiscountVolume t1(@Valid DiscountVolume discountVolume) {
        return discountVolumeQueryService.getResult(discountVolume.getType(), discountVolume.getResourceId());
    }

    @GetMapping("/t2")
    public DiscountVolume t2(@Valid DiscountVolume discountVolume) {
        String resourceType = discountVolume.getType();
        DiscountVolumeEnum discountVolumeEnum = DiscountVolumeEnum.valueOf(resourceType);
        DisCountVolumeManager manager = null;
        switch (discountVolumeEnum) {
            case SHOPPING:
                manager = new DisCountVolumeManager(new ShoppingStrategy());
                break;
            case RED_PAPER:
                manager = new DisCountVolumeManager(new RedPaperStrategy());
                break;
            case QQ_VIP:
                manager = new DisCountVolumeManager(new QQVipStrategy());
                break;
            default:
                break;
        }
        return Objects.isNull(manager) ? null : manager.queryByResourceId(discountVolume.getResourceId());
    }
}
